package com.leoman.indexapi.service.Impl;

import com.leoman.indexapi.service.UserTaskApiService;
import com.leoman.post.dao.PostTpSubDataDao;
import com.leoman.post.dao.PostTypeDataDao;
import com.leoman.post.dao.TpPostSubDao;
import com.leoman.post.entity.PostBase;
import com.leoman.post.entity.PostTpSubData;
import com.leoman.post.entity.PostTypeData;
import com.leoman.post.entity.TpPostSub;
import com.leoman.resources.dao.ResourceDataDao;
import com.leoman.resources.dao.ResourceDetailDataDao;
import com.leoman.resources.entity.ResourceData;
import com.leoman.resources.entity.ResourceDetailData;
import com.leoman.systemsetting.dao.UserTaskDao;
import com.leoman.systemsetting.entity.UserTask;
import com.leoman.user.dao.UserInfoDao;
import com.leoman.user.dao.UserSignDao;
import com.leoman.user.entity.UserInfo;
import com.leoman.user.entity.UserSign;
import com.leoman.userapi.service.UserIntegralService;
import com.leoman.utils.DateUtils;
import com.leoman.welfare.dao.WelfareDataDao;
import com.leoman.welfare.entity.Welfare;
import com.leoman.welfare.entity.WelfareData;
import com.leoman.work.dao.WorkDataDao;
import com.leoman.work.entity.Work;
import com.leoman.work.entity.WorkData;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 日常任务实现
 * Created by Administrator on 2016/6/12.
 */
@Service
public class UserTaskServiceImplApi implements UserTaskApiService {

    @Autowired
    private UserTaskDao userTaskDao;

    @Autowired
    private UserInfoDao userInfoDao;

    @Autowired
    private UserIntegralService userIntegralService;

    @Autowired
    private UserSignDao userSignDao;

    @Autowired
    private PostTypeDataDao postTypeDataDao;

    @Autowired
    private PostTpSubDataDao postTpSubDataDao;

    @Autowired
    private TpPostSubDao tpPostSubDao;

    @Autowired
    private WorkDataDao workDataDao;

    @Autowired
    private WelfareDataDao welfareDataDao;

    @Autowired
    private ResourceDataDao resourceDataDao;

    @Autowired
    private ResourceDetailDataDao resourceDetailDataDao;

    @Override
    @Transactional
    public void setDefaultTask(Long userId) {
        List<UserTask> userTaskList = userTaskDao.findList(userId);

        if (null == userTaskList || userTaskList.size() == 0) {
            UserInfo userInfo = new UserInfo();
            userInfo.setId(userId);
            UserTask userTask;

            for (int i = 1; i < 7; i++) {
                userTask = new UserTask();
                userTask.setUserInfo(userInfo);
                userTask.setIsGet(0);
                userTask.setCount(0);
                userTask.setType(i);

                userTaskDao.save(userTask);
            }
        }
    }

    @Override
    public Integer finishTask(Long userId, Integer type, boolean flag) {
        UserTask userTask = userTaskDao.findOneByUserIdAndType(userId, type);
        UserInfo userInfo = userInfoDao.findOne(userId);

        if (!flag) {
            // 用户进行任务
            if (null == userTask) {
                userTask = new UserTask();
                userTask.setUserInfo(userInfo);
                userTask.setIsGet(0);
                userTask.setCount(1);
                userTask.setType(type);
            } else {
                switch (type) {
                    case 2:
                        // 点赞
                    case 3:
                        // 发评论
                    case 6:
                        // 发弹幕
                        userTask.setCount(userTask.getCount() + 1);
                        break;
                    case 1:
                        // 签到
                    case 4:
                        // 发帖子
                    case 5:
                        // 每日分享
                        userTask.setCount(1);
                        break;
                }
            }

            userTaskDao.save(userTask);

            return 0;
        } else {
            if (null != userTask && userTask.getIsGet() == 0) {
                // 用户完成任务后领取奖励
                switch (type) {
                    case 1:
                        // 签到
                        userIntegralService.changeCoin(userInfo, "每天签到获得5馒头", 5);

                        // 每连续签到10天多5点馒头
                        UserSign userSign = userSignDao.findOneByUserId(userId);
                        if (userSign.getDays() % 10 == 0) {
                            userIntegralService.changeCoin(userInfo, "连续签到10天额外获得5馒头", 5);
                        }
                        break;
                    case 2:
                        // 点赞
                        userIntegralService.changeCoin(userInfo, "每日点赞3次获得3个馒头", 3);
                        break;
                    case 3:
                        // 发评论
                        userIntegralService.changeCoin(userInfo, "每日评论帖子2次获得4个馒头", 4);
                        break;
                    case 4:
                        // 发帖子
                        userIntegralService.changeCoin(userInfo, "每日发布1个帖子获得5个馒头 ", 5);
                        break;
                    case 5:
                        // 每日分享
                        userIntegralService.changeCoin(userInfo, "每日分享1个帖子获得5个馒头 ", 5);
                        break;
                }

                // 修改任务领取状态
                userTask.setIsGet(1);
                userTaskDao.save(userTask);
                return 0;
            } else {
                return 1;
            }
        }
    }

    @Override
    public List<UserTask> findTaskList(Long userId) {
        return userTaskDao.findListByUserId(userId);
    }

    @Override
    @Transactional
    public void setDefaultTask() {
        List<UserInfo> userList = userTaskDao.findListWithOutTask();

        for (UserInfo userInfo : userList) {
            setDefaultTask(userInfo.getId());
        }
    }

    @Override
    public UserTask findInfoByUserIdAndType(Long userId, Integer type) {
        return userTaskDao.findOneByUserIdAndType(userId, type);
    }

    @Override
    public void addPostPlayNum(PostBase postBase, Integer playNum, Integer sendNum) {
        String createDate = DateUtils.getTodayDate();

        PostTypeData postTypeData = postTypeDataDao.findOneByCategoryIdAndCreateDate(postBase.getCategory().getId(), createDate);

        if (null == postTypeData) {
            postTypeData = new PostTypeData();
            postTypeData.setPlayNum(playNum);
            postTypeData.setSendNum(sendNum);
            postTypeData.setCreateDate(createDate);
            postTypeData.setCategory(postBase.getCategory());
        } else {
            postTypeData.setPlayNum(postTypeData.getPlayNum() + playNum);
            postTypeData.setSendNum(postTypeData.getSendNum() + sendNum);
        }

        postTypeDataDao.save(postTypeData);
    }

    @Override
    public void addPostTpSubData(PostBase postBase, UserInfo userInfo, String tpSubIds) {
        PostTpSubData postTpSubData = new PostTpSubData();
        postTpSubData.setPostBase(postBase);
        postTpSubData.setUserInfo(userInfo);
        postTpSubData.setTpSubIds(tpSubIds);

        String tpSubNames = "";
        String[] ids = tpSubIds.split(",");
        TpPostSub tpPostSub;
        for (String id : ids) {
            if (StringUtils.isBlank(id)) {
                continue;
            }
            tpPostSub = tpPostSubDao.findOne(Long.parseLong(id));
            tpSubNames += tpPostSub.getName() + ",";
        }

        if (tpSubNames.length() > 0) {
            tpSubNames = tpSubNames.substring(0, tpSubNames.length() - 1);
        }

        postTpSubData.setTpSubNames(tpSubNames);

        postTpSubDataDao.save(postTpSubData);
    }

    @Override
    public void addWorkData(Work work, Long detailId, Integer playNum, Integer barrageNum) {
        String createDate = DateUtils.getTodayDate();

        WorkData workData = workDataDao.findOneByDetailIdAndCreateDate(work.getId(), detailId, createDate);

        if (null == workData) {
            workData = new WorkData();
            workData.setWork(work);
            workData.setDetailId(detailId);
            workData.setPlayNum(playNum);
            workData.setBarrageNum(barrageNum);
            workData.setCreateDate(createDate);
        } else {
            workData.setPlayNum(workData.getPlayNum() + playNum);
            workData.setBarrageNum(workData.getBarrageNum() + barrageNum);
        }

        workDataDao.save(workData);
    }

    @Override
    public void addWelfareData(Welfare welfare, Integer playNum) {
        String createDate = DateUtils.getTodayDate();

        WelfareData welfareData = welfareDataDao.findOneByWelfareIdAndCreateDate(welfare.getId(), createDate);

        if (null == welfareData) {
            welfareData = new WelfareData();
            welfareData.setWelfare(welfare);
            welfareData.setPlayNum(playNum);
            welfareData.setCreateDate(createDate);
        } else {
            welfareData.setPlayNum(welfareData.getPlayNum() + playNum);
        }

        welfareDataDao.save(welfareData);
    }

    @Override
    public void addResourceData(Long resourceId, String resourceName, Integer type, Integer playNum, Integer downloadNum) {
        String createDate = DateUtils.getTodayDate();

        ResourceData resourceData = resourceDataDao.findOneByParams(resourceId, type, createDate);

        if (null == resourceData) {
            resourceData = new ResourceData();
            resourceData.setResourceId(resourceId);
            resourceData.setResourceName(resourceName);
            resourceData.setType(type);
            resourceData.setPlayNum(playNum);
            resourceData.setDownloadNum(downloadNum);
            resourceData.setCreateDate(createDate);
        } else {
            resourceData.setPlayNum(resourceData.getPlayNum() + playNum);
            resourceData.setDownloadNum(resourceData.getDownloadNum() + downloadNum);
        }

        resourceDataDao.save(resourceData);
    }

    @Override
    public void addResourceDetailData(Long resourceId, Integer type, UserInfo userInfo, String content) {
        ResourceDetailData resourceDetailData = new ResourceDetailData();
        resourceDetailData.setResourceId(resourceId);
        resourceDetailData.setType(type);
        resourceDetailData.setUserInfo(userInfo);
        resourceDetailData.setContent(content);

        resourceDetailDataDao.save(resourceDetailData);
    }
}
